package homework;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author DCSGO
 * @version 1.0
 * 定义个泛型类DAO<T>，在其中定义一个Map成员变量，Map的键为String类型，值为T类型。
 * 分别创建以下方法:
 * (1) public void save(String id,T entity):保存T类型的对象到Map成员变量中
 * (2) public T get(String id): 从map中获取id对应的对象
 * (3) public void update(String id,T entity):替换map中key为id的内容,改为entity对象
 * (4) public List<T> list():返回map中存放的所有T对象
 * (5)public void delete(String id):删除指定id对象
 * 定义一个User类:
 * 该类包含:private成员变量(String类型)id,age;(String类型) name。
 * 创建 DAO类的对象，分别调用其save、get、update、list、delete方法来操作User对象，使用Junit 单元测试类进行测试。
 */
public class Homework01 {

    @Test
    public void test_Dao() {
        User user1=new User("001",18,"张燕");
        User user2=new User("002",18,"王强");
        User user3=new User("003",18,"徐小明");
        DAO<User> dao = new DAO<>();
        dao.save(user1.getId(),user1);
        dao.save(user2.getId(),user2);
        dao.save(user3.getId(),user3);

        System.out.println(dao.list());
        //[
        //User{id='001', age=18, name='张燕'},
        //User{id='002', age=18, name='王强'},
        //User{id='003', age=18, name='徐小明'}]

        System.out.println(dao.get("002"));
        //User{id='002', age=18, name='王强'}

        dao.update(user2.getId(),new User("002",19,"强哥"));
        System.out.println(dao.list());
        //[
        //User{id='001', age=18, name='张燕'},
        //User{id='002', age=19, name='强哥'},
        //User{id='003', age=18, name='徐小明'}]

        dao.delete(user3.getId());
        System.out.println(dao.list());

        //[
        //User{id='001', age=18, name='张燕'},
        //User{id='002', age=19, name='强哥'}]
    }
}

class User {
    private String id;
    private int age;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "\nUser{" +
                "id='" + id + '\'' +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public int getAge() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(id, user.id) && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, age, name);
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(String id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
}

class DAO<T> {
    Map<String, T> map;

    public void save(String id, T entity) {
        map.put(id, entity);
    }

    public T get(String id) {
        return map.get(id);
    }

    public void update(String id, T entity) {
        map.put(id, entity);
    }

    public List<T> list() {
        return new ArrayList<T>(map.values());
    }

    public void delete(String id) {
        map.remove(id);
    }

    public DAO() {
        this.map = new HashMap<String, T>();
    }
}
